package com.uenpay.im.invokeitems;

import android.text.TextUtils;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.utils.StringUtils;
import com.qyx.android.message.PushServiceShareData;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetSocketInvokeItem extends HttpInvokeItem {
	public class GetSocketResult {
		public int status;
		public String ip;
		public int port;
		public String msg;
	}

	public GetSocketResult getOutput() {
		return (GetSocketResult) GetResultObject();
	}

	public GetSocketInvokeItem() {
		// SetUrl(APIConfiguration.getTalkIpBaseUrl() + "chat?type=1&cust_id="
		// + PushServiceShareData.getInstance().getCustId());
		// http://api.qiyunxin.com/Cust/Cust/getServer?ip=&logintype=
		String ip = getLocalHostIp();
		String url = APIConfiguration.getBaseUrl() + "Cust/Cust/getServer?ip="
				+ StringUtils.encodeParams(ip) + "&logintype=1&_custid="
				+ PushServiceShareData.getInstance().getCustId() + "&_token="
				+ PushServiceShareData.getInstance().getToken();
		SetUrl(url);
		// SetUrl(APIConfiguration.getBaseUrl() + "Cust/Init/init");
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetSocketResult result = new GetSocketResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			JSONObject jo = json.optJSONObject("data");
			if (jo != null) {
				JSONObject socketJson = jo.optJSONObject("chat");
				if (socketJson != null) {
					result.ip = socketJson.optString("host");
					result.port = socketJson.optInt("port");
				}

				if (jo.has("roomserver")) {
					String roomserver = jo.optString("roomserver");
					if(!TextUtils.isEmpty(roomserver)){
						IMApplication.config.setRoomServer(roomserver);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	// @Override
	// protected Object DeserializeResult(String responseBody) {
	// GetSocketResult result = new GetSocketResult();
	// try {
	// JSONObject json = new JSONObject(responseBody);
	// JSONObject jo = json.optJSONObject("res");
	// if (jo != null) {
	// if (jo != null) {
	// result.ip = jo.optString("ip");
	// result.port = jo.optInt("port");
	// }
	// }
	// } catch (JSONException e) {
	// e.printStackTrace();
	// return result;
	// }
	//
	// return result;
	// }

	public String getLocalHostIp() {
		String ipaddress = "";
		try {
			Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces();
			while (en.hasMoreElements()) {
				NetworkInterface nif = en.nextElement();
				Enumeration<InetAddress> inet = nif.getInetAddresses();
				while (inet.hasMoreElements()) {
					InetAddress ip = inet.nextElement();
					if (!ip.isLoopbackAddress()) {
						return ipaddress = ip.getHostAddress();
					}
				}

			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return ipaddress;

	}
}